This class came about when I began to get heavily involved with uploading and working data for clients. I created another class that turns a csv file into it's own mysql database basically, and the need arose to separate my validation routines from the Form class they used to reside in.
These are the validation routines available:
'a' - An alpha (letters only) value
'an' - An alphanumeric (letters and numbers) value
'ap' - Letters and basic puncutuation
'anp' - An alphanumeric value with punctuation
'num' - A positive or negative number
'nummin' - A minimum number
'nummax' - A maximum number
'numrange' - A number within a range
'int' - A whole number greater than 0
'intmin' - A minimum integer
'intmax' - A maximum integer
'intrange' - An integer within a range
'float' - A positive number with a possible decimal (commas will be stripped)
'floatmin' - A minimum float
'floatmax' - A maximum float
'floatrange' - A float within a range
'string' - No extraneous spaces or code
'stringmin' - A string with a minimum number of characters
'stringmax' - A string with a maximum number of characters
'stringrange' - A string within a minimum and a maximum number of characters
'wordmin' - A string with a minimum number of words contained
'wordmax' - A string with a maximum number of words contained
'datetime' - A mysql formatted datetime value
'date' - A mysql formatted date in the format of 'Y-m-d'
'email' - A valid email address (according to syntax only)
'pass' - A password of at least 6 characters and no spaces
'url' - A valid website address including the 'http://' portion
'phone' - A 7 or 10 digit phone number
'zip' - A Canada, UK, or US zip code
'ssn' - A social security number
'cc' - A credit card number
'YN' - yes (Y) or no (N)
array() - An array of expected results such as array('yes', 'no', 'maybe', 'so'), etc.
I also added some other frequently used validation methods that allow you to:
- Remove extra whitespace
- Replace newlines with <br /> tags
- Disable HTML and other markup
- Properly capitalize titles
- Nicely truncate a string
- Generate an SEO value
- Quickly columnize an array of data
Validating data can be an ugly uphill battle. This class makes it a breeze.
Creates a new validation object.
| Example | |
This method will filter and validate your data one value at a time. This is the method used in the Form class for all of the validation routines it performs.
| $filter | The type of data you're expecting. To include parameters make your filter an array. See examples. |
| $data | The value you want to filter or validate. |
| Returns | The value you expect (if possible), or nothing at all (if it's a string value). A number's default is 0, and the yes or no ('YN') default is 'N'. Credit card numbers include what type it is. |
| Example | |
This is for when you want to take a whole bunch of data and analyze each new line (or however else you want to delimit it), and just get the emails out of it for instance.
| $filter | The same rules and routines apply as in $this->data(). |
| $data | The data you want to work from. This data is ran through $this->strip($data) where all new lines are converted to <br /> tags, and every other tag is taken out. |
| $delimiter | How you want to break up your $data for validation. |
| $return | The default is whatever you used for the $delimiter. If you want to return your string in some other form then say so here. |
| Returns | By default this method returns '<br />' separated values, and any duplicates found are taken out. |
| Example | |
This method is just like the strip_tags() function only we replace all newlines with '<br />' tags, and we remove any doubled-up whitespace and all tags (besides the ones you want to keep). This is usually only used when you're expecting a one-liner of information.
| $data | What you want to strip down. |
| $tags | Any tags you want to keep. |
| Returns | A stripped and sanitized string. |
| Example | |
This method takes a string and sanitizes it basically. It replaces all newlines to '<br />' tags, and converts special characters to their HTML entities.
| $data | A string you're not too sure about. |
| Returns | A string you can feel safe and secure with. |
| Example | |
Performs proper capitalization on titles.
| $string | The title you would like to format. |
| Returns | A properly capitalized title. |
| Example | |
Truncates a bite out of text without cutting a word in half.
| $string | What you want to chop down to size |
| $max | The maximum number of characters you would like to display. |
| $moretext | How you want to indicate that the text has been truncated. |
| Returns | Your truncated text. |
| Example | |
When it comes to SEO friendly strings - less is more. This function will throw out the worthless words, accents and entities to give you something you can feed the bots. Mainly this is used to generate a search engine friendly url.
| $string | What you want to tell the search engines. |
| $implode | How you want to tie the words together. |
| Returns | Your SEO friendly string. |
| Example | |
This will pad a multidimensional array of data to be displayed as columns.
| $array | A multidimensional array of data. See example. |
| $spacing | How many spaces you would like between columns. |
| Returns | A string of code to add to your $html. |
| Example | |
This will validate your reCAPTCHA and check for an appropriate response.
| Returns | An error string that will be empty if they entered the reCAPTCHA correctly. |
| Example | |
This generates the jQuery code that will validate your form on the client side using the jQuery Validation plugin. It automatically (using the Page class) inserts the code into the head section of your page.
| $form | The name (id) of the form. |
| $check | An array ('name'=>'filter') of the form values you want returned. |
| $required | An array of required form input names. |
| $upload | An array ('name'=>'types') of files to upload. 'Types' can also be it's own array. |
| Example | |
This method validates and filters your form's submitted data.
| $type | A key=>value pair in the form of ($type=>$form) where $type is either 'get' or 'post', and $form is the form's name you want to validate. |
| $check | An array ('name'=>'filter') of the form values you want returned. |
| $required | An array of required form input names, ie. these fields must not be empty. |
| Returns | An array of 3 values: 1) A $vars['name'] = $value array of all of the $form->check(), $form->process(), and $form->upload() methods previously called. 2) An $errors array that you don't need to worry about. You only need to know that it is empty which means it is safe to process all $vars. 3) An $eject string to know where to send the user so that they can't hurt themselves by resubmitting the same form over and over again when they click their browsers back button. |
| Example | |
This processes a form's file field, and attempts to upload it to your uploads directory.
| $file | The name of file field you want to upload. |
| $types | The type(s) of uploaded file(s) you're allowing. Available types include: 'jpg' - jpeg, jpg, pjpeg 'gif' 'png' 'bmp' - bitmap 'swf' - flash 'doc' - ms word 'txt' 'pdf' 'ppt' - powerpoint 'xls' - excel 'xml' 'mpeg' - all types 'mov' - quicktime videos 'zip' |
| Returns | An array($filename, $error). If $filename !== false then this operation was successful. If (!empty($error)) then something went wrong, and we may have an idea of why. |
| Example | |